package com.bl.basic.rocketmq;

import com.bl.order.domain.OrderPetAcquisition;
import com.bl.order.mapper.OrderPetAcquisitionMapper;
import com.bl.pet.mapper.PetMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",selectorExpression="*",consumerGroup = "queue_group_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {
    @Autowired
    private  OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();

        String msg = new String(body, CharsetUtil.UTF_8);
        log.info("接收到消息：{}", msg);
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadByOrderSn(msg);
        if (order.getState() == 0 ){
            order.setState(-1);
            order.setShopId(null);
            orderPetAcquisitionMapper.update(order);
            petMapper.remove(order.getPetId());
            System.out.println("订单取消");
        }



    }




}